#include "serialconfigdialog.h"
#include "sqlitemanage.h"

SerialConfigDialog::SerialConfigDialog(QWidget *parent) : QDialog(parent)
{
    setWindowTitle("添加串口配置");
    setMinimumWidth(400);

    // 创建表单布局
    QFormLayout *formLayout = new QFormLayout(this);

    // 配置名称
    m_nameEdit = new QLineEdit(this);
    formLayout->addRow("配置名称:", m_nameEdit);

    // 串口名称
    m_portNameCombo = new QComboBox(this);
    // 获取可用串口
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        m_portNameCombo->addItem(info.portName());
    }
    formLayout->addRow("串口名称:", m_portNameCombo);

    // 波特率
    m_baudRateCombo = new QComboBox(this);
    m_baudRateCombo->addItem("9600", QSerialPort::Baud9600);
    m_baudRateCombo->addItem("115200", QSerialPort::Baud115200);
    m_baudRateCombo->addItem("57600", QSerialPort::Baud57600);
    m_baudRateCombo->addItem("38400", QSerialPort::Baud38400);
    m_baudRateCombo->setCurrentIndex(1); // 默认115200
    formLayout->addRow("波特率:", m_baudRateCombo);

    // 数据位
    m_dataBitsCombo = new QComboBox(this);
    m_dataBitsCombo->addItem("8位", QSerialPort::Data8);
    m_dataBitsCombo->addItem("7位", QSerialPort::Data7);
    m_dataBitsCombo->addItem("6位", QSerialPort::Data6);
    m_dataBitsCombo->addItem("5位", QSerialPort::Data5);
    m_dataBitsCombo->setCurrentIndex(0); // 默认8位
    formLayout->addRow("数据位:", m_dataBitsCombo);

    // 校验位
    m_parityCombo = new QComboBox(this);
    m_parityCombo->addItem("无校验", QSerialPort::NoParity);
    m_parityCombo->addItem("奇校验", QSerialPort::OddParity);
    m_parityCombo->addItem("偶校验", QSerialPort::EvenParity);
    m_parityCombo->setCurrentIndex(0); // 默认无校验
    formLayout->addRow("校验位:", m_parityCombo);

    // 停止位
    m_stopBitsCombo = new QComboBox(this);
    m_stopBitsCombo->addItem("1位", QSerialPort::OneStop);
    m_stopBitsCombo->addItem("1.5位", QSerialPort::OneAndHalfStop);
    m_stopBitsCombo->addItem("2位", QSerialPort::TwoStop);
    m_stopBitsCombo->setCurrentIndex(0); // 默认1位
    formLayout->addRow("停止位:", m_stopBitsCombo);

    // 流控制
    m_flowControlCombo = new QComboBox(this);
    m_flowControlCombo->addItem("无流控", QSerialPort::NoFlowControl);
    m_flowControlCombo->addItem("硬件流控", QSerialPort::HardwareControl);
    m_flowControlCombo->addItem("软件流控", QSerialPort::SoftwareControl);
    m_flowControlCombo->setCurrentIndex(0); // 默认无流控
    formLayout->addRow("流控制:", m_flowControlCombo);

    // 创建按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();

    m_okButton = new QPushButton("确定", this);
    connect(m_okButton, &QPushButton::clicked, this, &SerialConfigDialog::accept);

    m_cancelButton = new QPushButton("取消", this);
    connect(m_cancelButton, &QPushButton::clicked, this, &SerialConfigDialog::reject);

    buttonLayout->addWidget(m_okButton);
    buttonLayout->addWidget(m_cancelButton);

    formLayout->addRow(buttonLayout);
}

SerialConfig SerialConfigDialog::getConfig() const
{
    SerialConfig config;
    config.name = m_nameEdit->text();
    config.portName = m_portNameCombo->currentText();
    config.baudRate = m_baudRateCombo->currentData().toInt();
    config.dataBits = static_cast<QSerialPort::DataBits>(m_dataBitsCombo->currentData().toInt());
    config.parity = static_cast<QSerialPort::Parity>(m_parityCombo->currentData().toInt());
    config.stopBits = static_cast<QSerialPort::StopBits>(m_stopBitsCombo->currentData().toInt());
    config.flowControl = static_cast<QSerialPort::FlowControl>(m_flowControlCombo->currentData().toInt());

    return config;
}

void SerialConfigDialog::setConfig(const SerialConfig &config)
{
    m_nameEdit->setText(config.name);
    m_portNameCombo->setCurrentText(config.portName);

    // 设置波特率
    for (int i = 0; i < m_baudRateCombo->count(); ++i) {
        if (m_baudRateCombo->itemData(i).toInt() == config.baudRate) {
            m_baudRateCombo->setCurrentIndex(i);
            break;
        }
    }

    // 设置数据位
    for (int i = 0; i < m_dataBitsCombo->count(); ++i) {
        if (m_dataBitsCombo->itemData(i).toInt() == static_cast<int>(config.dataBits)) {
            m_dataBitsCombo->setCurrentIndex(i);
            break;
        }
    }

    // 设置校验位
    for (int i = 0; i < m_parityCombo->count(); ++i) {
        if (m_parityCombo->itemData(i).toInt() == static_cast<int>(config.parity)) {
            m_parityCombo->setCurrentIndex(i);
            break;
        }
    }

    // 设置停止位
    for (int i = 0; i < m_stopBitsCombo->count(); ++i) {
        if (m_stopBitsCombo->itemData(i).toInt() == static_cast<int>(config.stopBits)) {
            m_stopBitsCombo->setCurrentIndex(i);
            break;
        }
    }

    // 设置流控制
    for (int i = 0; i < m_flowControlCombo->count(); ++i) {
        if (m_flowControlCombo->itemData(i).toInt() == static_cast<int>(config.flowControl)) {
            m_flowControlCombo->setCurrentIndex(i);
            break;
        }
    }
}
